import {createRouter, createWebHashHistory} from 'vue-router'
import Home from '@/views/Home.vue'
import login from "@/views/login";
import Session from "@/utils/storage";

const routes = [
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {path: '/home/carousel-map-management', component: () => import('@/components/CarouselMapManagement.vue')},
      {path: '/home/contract-performance-statistics', component: () => import('@/components/ContractPerformanceStatistics.vue')},
      {path: '/home/data-overview', component: () => import('@/components/DataOverview.vue')},
      {path: '/home/doctor-management', component: () => import('@/components/DoctorManagement.vue')},
      {path: '/home/information-management', component: () => import('@/components/InformationManagement.vue')},
      {path: '/home/message-management', component: () => import('@/components/MessageManagement.vue')},
      {path: '/home/operation-log', component: () => import('@/components/OperationLog.vue')},
      {path: '/home/organizational-management', component: () => import('@/components/OrganizationalManagement.vue')},
      {path: '/home/pending', component: () => import('@/components/ContractPendingList.vue')},
      {path: '/home/pending-service-management', component: () => import('@/components/PendingServiceManagement.vue')},
      {path: '/home/processed', component: () => import('@/components/ContractProcessedList.vue')},
      {path: '/home/protocol-management', component: () => import('@/components/ProtocolManagement.vue')},
      {path: '/home/resident-management', component: () => import('@/components/ResidentManagement.vue')},
      {path: '/home/role-management', component: () => import('@/components/RoleManagement.vue')},
      {path: '/home/service-item-management', component: () => import('@/components/ServiceItemManagement.vue')},
      {path: '/home/service-package-management', component: () => import('@/components/ServicePackageManagement.vue')},
      {path: '/home/service-performance-statistics',component: () => import('@/components/ServicePerformanceStatistics.vue')},
      {path: '/home/service-record', component: () => import('@/components/ServiceRecord.vue')},
      {path: '/home/system-announcement', component: () => import('@/components/SystemAnnouncement.vue')},
      {path: '/home/tag-management', component: () => import('@/components/TagManagement.vue')},
      {path: '/home/team-management', component: () => import('@/components/TeamManagement.vue')},
      {path: '/home/workbench', component: () => import('@/components/Workbench.vue')}
    ]
  },
  {
    path: '/',
    name: 'login',
    component: login
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  //获取sessionStorage中是后端返回的token
  const token = Session.get('token')
  //如果用户访问的是敏感地址，且token不存在
  if (to.path === '/home' && !token) {
    //不予放行，并导航到登录的路由地址
    next('/')
  } else {
    next()
  }
})


export default router
